package com.liangtian.mdm.system.log.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.DateUtils;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.system.log.model.SysLog;
import com.liangtian.mdm.system.log.model.SysLogEnum;
import com.liangtian.mdm.system.log.service.SysLogService;
import com.liangtian.mdm.util.RequestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name: com.liangtian.mdm.system.log.controller </p>
 * <p>Project Name: chccig-dm </p>
 * <p>Copyright   :  Copyright(c) 2016 石化盈科信息技术有限责任公司 </p>
 * <p>Created Time: 2016-05-20 14:54</p>
 * <p>Description : 操作日志控制层</p>
 *
 * @author : beamzhang
 * @version :  1.0
 */
@Controller
@RequestMapping("/sysLog")
public class SysLogController extends BaseController {

    //日志打印对象
    private static final Logger logger = LoggerFactory.getLogger(SysLogController.class);

    //注入日志的服务接口对象
    @Autowired
    SysLogService sysLogServiceImpl;

//    @Autowired
//    MailService mailServiceImpl;


    private List<SysLog> sysListByExport = null;
    private Map<String, Object> sysmap = null;

    /**
     * @return 日志页面
     * @desc 跳转至日志列表页面
     */
    @RequestMapping(value = "/showSysLogListView", method = RequestMethod.GET)
    public ModelAndView showSysLogListView() {
        view.setViewName("system/sysLog/page/sysLog_list");
        return view;
    }

    /**
     * @param sysLog 日志对象
     * @return 日志列表对象类表
     * @desc 获取日志列表数据
     */
    @RequestMapping(value = "/queryAllSysLogList", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject queryAllSysLogList(SysLog sysLog, String beginDateTime, String endDateTime) {
        ResultObject result = new ResultObject();
        if (null != beginDateTime && !"".equals(beginDateTime)) {
            sysLog.setBeginDate(DateUtils.stringToDate(beginDateTime, "yyyy-MM-dd"));
        }
        if (null != endDateTime && !"".equals(endDateTime)) {
            sysLog.setEndDate(DateUtils.stringToDate(endDateTime, "yyyy-MM-dd"));
        }
        PageInfo pageInfo = sysLogServiceImpl.selectLogPageByCondition(sysLog);
        result.setData(pageInfo.getList());
        result.setRecordsTotal(pageInfo.getTotal());
        result.setRecordsFiltered(pageInfo.getTotal());
        result.setDraw(sysLog.getDraw());
        //===============发送邮件测试 start==============
//        try {
//            Mail mail = new Mail();
//            request.setAttribute("domain","liuhp1641");
//            StringBuffer code = new StringBuffer();
//            code.append("email=").append(Encoder.symmetricEncrypto("11111@qq.com")).append("&amp;");
//            code.append("domain=").append(Encoder.symmetricEncrypto("www.baidu.com")).append("&amp;");
//            code.append("loginAccount=").append(Encoder.symmetricEncrypto("123456"));
//            mail.setTo("522501392@qq.com");
//            mail.setSubject("邮箱验证,欢迎使用陕煤化数据平台!");
//            mail.setContent(mailServiceImpl.getTemplateContentFromFile("mail_template").replace("#loginAccount#",
//                    "123456").replace("#CODE#", code).replace("#email#","11111@qq.com@qq.com").replace("#email#","11111@qq.com"));
//            mailServiceImpl.sendMailAsyn(mail);
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
        //===============发送邮件测试 end==============
        return result;
    }

    /**
     * @param sysLog 日志对象
     * @return 导出数据结果
     * @desc 导出操作日志数据
     */
    @RequestMapping(value = "/exportLogsData", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject exportLogsData(SysLog sysLog, String beginDateTime, String endDateTime) {
        ResultObject result = new ResultObject();
        if (null != beginDateTime && !"".equals(beginDateTime)) {
            sysLog.setBeginDate(DateUtils.stringToDate(beginDateTime, "yyyy-MM-dd"));
        }
        if (null != endDateTime && !"".equals(endDateTime)) {
            sysLog.setEndDate(DateUtils.stringToDate(endDateTime, "yyyy-MM-dd"));
        }
        //获取项目的所有日志记录
        int count = sysLogServiceImpl.exportLogsData(sysLog);
        if (count > 0) {
            result.setMsg("导出成功");
            result.setSuccess(true);
        } else {
            result.setMsg("导出失败");
            result.setSuccess(false);
        }
        return result;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/queryAllLogList", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryAllLogList(HttpServletRequest request, SysLog syslog) {
        Map paramMap = RequestUtils.paramToMap(request, SysLog.class, false);
        PageInfo pageInfo = sysLogServiceImpl.getSysLogList(paramMap);
        List l = pageInfo.getList();
        sysListByExport = pageInfo.getList();
        sysmap = paramMap;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", l);// JSONArray
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/getType", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject getType(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        List<String> operTypeList = null;
        List<String> wlTypeList = null;
        List<String> sysTypeList = null;

        try {
            operTypeList = SysLogEnum.getOperTypeList();
            wlTypeList = SysLogEnum.getWlTypeList();
            sysTypeList = SysLogEnum.getSysTypeList();

        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("operTypeList", operTypeList);
        jsonObject.put("wlTypeList", wlTypeList);
        System.out.println(wlTypeList);
        jsonObject.put("sysTypeList", sysTypeList);
        System.out.println(sysTypeList);
        return jsonObject;
    }


}
